﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
namespace XiaoYuStock.Core.Mails
{
    public class MyEmail
    {

        static string smtpHost = "smtp.163.com";

        static string from = "www5uzhcom@163.com";

        static string pass = "5uzhcomu";

        static string displayName = "吾优之汇";

        public static void SendMail(string subject, string body,string attachContent, params string [] toMails)
        {
            try
            {
                MailMessage mail = new MailMessage();
                if (toMails != null && toMails.Length > 0)
                {
                    foreach (var s in toMails)
                    { 
                     mail.To.Add(s);
                    }
                }
                mail.IsBodyHtml = false;
                mail.Subject = subject;
                mail.Body = body;
                mail.SubjectEncoding = Encoding.GetEncoding("gb2312");
                mail.BodyEncoding = Encoding.GetEncoding("gb2312");
                MemoryStream stream = null;
                if (!string.IsNullOrWhiteSpace(attachContent))
                {
                    byte[] array = Encoding.GetEncoding("gb2312").GetBytes(attachContent);
                    stream = new MemoryStream(array);
                    mail.Attachments.Add(new Attachment(stream, "data.txt"));
                    
                   
                }
                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                mail.From = new System.Net.Mail.MailAddress(from, displayName);

                SmtpClient smtp = new SmtpClient(smtpHost);
                smtp.Credentials = new System.Net.NetworkCredential(from, pass);
                smtp.Send(mail);
                mail.Dispose();
                if (stream != null)
                    stream.Close();
                    
            }
            catch (Exception ex)
            {
                LogHelper.ErrorAsync("发送邮件失败:"+ex.Message);
            }

        }

        /// <summary>
        /// 阿里云服务器屏蔽了端口为25的邮件端口
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="attachContent"></param>
        /// <param name="toMails"></param>
        public static void SendMail2(string subject, string body, string attachContent, params string[] toMails)
        {
            try
            {
                System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
                if (toMails != null && toMails.Length > 0)
                {
                    mail.To = string.Join(";", toMails);
                }
                mail.BodyFormat = System.Web.Mail.MailFormat.Html;
                mail.Subject = subject;
                mail.Body = body;
                mail.BodyEncoding = Encoding.GetEncoding("gb2312");
                MemoryStream stream = null;
                if (!string.IsNullOrWhiteSpace(attachContent))
                {
                    byte[] array = Encoding.GetEncoding("gb2312").GetBytes(attachContent);
                    stream = new MemoryStream(array);
                    mail.Attachments.Add(new Attachment(stream, "data.txt"));


                }
                mail.From = "\\"+displayName+"\\ <"+from+">"; 
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",from); //邮箱登录账号，这里跟前面的发送账号一样就行  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass); //这个密码要注意：如果是一般账号，要用授权码；企业账号用登录密码  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密  
                System.Web.Mail.SmtpMail.SmtpServer = smtpHost;    //企业账号用smtp.exmail.qq.com  
                System.Web.Mail.SmtpMail.Send(mail);

            }
            catch (Exception ex)
            {
                LogHelper.ErrorAsync("发送邮件失败:" + ex.Message);
            }
        }


        ///<summary>
        /// 添加附件
        ///</summary>
        ///<param name="attachmentsPath">附件的路径集合，以分号分隔</param>
        public void AddAttachments(string attachmentsPath)
        {
            try
            {
                string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
                Attachment data;
                ContentDisposition disposition;
                for (int i = 0; i < path.Length; i++)
                {
                    data = new Attachment(path[i], MediaTypeNames.Application.Octet);
                    disposition = data.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(path[i]);
                    disposition.ModificationDate = File.GetLastWriteTime(path[i]);
                    disposition.ReadDate = File.GetLastAccessTime(path[i]);
                    //mMailMessage.Attachments.Add(data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    
    }
}
